package com.jike.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jike.exception.UserNotFound;
import com.jike.model.User;
import com.jike.service.UserService;

public class Client {
	public static void StringBulider(User user) {
		StringBuilder sb = new StringBuilder();
		sb.append("id:" + user.getId());
		sb.append("name:" + user.getName());
		sb.append("address:" + user.getAddress());
		sb.append("phone:" + user.getPhone());
		System.out.println(">>>>>>" + sb.toString());
	}

	public static void testList(UserService userService) {
		List<User> users = userService.getUsers();
		if (null != users) {
			for (int i = 0; i < users.size(); i++) {
				StringBulider(users.get(i));
			}
		}
	}

	public static void testUpdate(UserService userService) throws UserNotFound {
		User user = new User();
		user.setId(1);
		user.setAddress("北京路122号");
		userService.updateUser(user);
	}

	public static void testdelete(UserService userService) throws UserNotFound {
		User user = new User();
		user.setId(1);
		userService.deleteUser(user);
	}

	public static void testSave(UserService userService) {
		User user = new User();
		user.setId(7);
		user.setAddress("ddddddddddd");
		user.setName("wxx");
		user.setPhone("111111");
		userService.add(user);
		
	}

	public static void main(String[] args) throws UserNotFound {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService us = (UserService) ac.getBean("userService");
		testList(us);
		//testUpdate(us);
		//testdelete(us);
		testSave(us);
		testList(us);
	}
}
